/**
 * Created by tangqiaojie <2482366539@qq.com> on 2020/4/7 0007
 */
import { sendSbList, findIndexButton } from './tool.js';
import { insertMoment, nextTick } from '../../lib/utils.js';


const handleSmallIndex = (currentZeroInfo)=>{
  window.setTimeout(()=>{
    window.location.reload();
  }, 30000);
  let zeroStep = window.localStorage.getItem('zeroStep');
  let zzsReady = window.localStorage.getItem('zzsReady');
  let yhsReady = window.localStorage.getItem('yhs');
  let incomeReady = window.localStorage.getItem('incomeReady');
  let zzsObj = findIndexButton('增值税小规模纳税人月(季)度申报');
  let incomeObj = findIndexButton('居民企业(查账征收)所得税月（季）申报');
  let yhsObj = findIndexButton('印花税申报');
  if (!zzsReady) {
    window.localStorage.setItem('zzsReady', 1);
    zzsObj.txt === '申报' && zzsObj.btn.click();
  }
  if (!incomeReady) {
    window.localStorage.setItem('incomeReady', 1);
    incomeObj.txt === '申报' && incomeObj.btn.click();
  }
  if (yhsReady) {
    yhsObj.txt === '查看';
  }
  let isZzsReport = !zzsObj.btn ? true : (zzsObj.txt === '查看');
  let isIncomeReport = !incomeObj.btn ? true : (incomeObj.txt === '查看');
  let isYhsReport = !yhsObj.btn ? true : (yhsObj.txt === '查看');
  if (isZzsReport && isIncomeReport && isYhsReport) {
    insertMoment();
    nextTick(()=>{
      if (window.moment) {
        sendSbList(currentZeroInfo);
        return true;
      }
    });
  }
};

const handleSmallStep3 = ()=>{

};

const handleSmallStep2 = ()=>{

};

const handleSmallStep1 = ()=>{
  nextTick(()=>{
    if ($('#contentiframe').contents().find('#ente_home').length) {
      $('#contentiframe')[0].contentWindow.entryClickSb();
      nextTick(()=>{
        if ($('#contentiframe').contents().find('#mainIframe').contents()
          .find('#btn10406').length) {
          let win = $('#contentiframe').contents().find('#mainIframe')[0].contentWindow;
          if (win.$('#btsblb li:eq(1) .state').html().trim() === '已填写') {
            introMainTable(win);
          } else {
            win.initData('initData');
            nextTick(()=>{
              if (win.$('.xubox_msg.xubox_text').length && win.$('.xubox_msg.xubox_text').html() == '初始化成功!') {
                introMainTable(win);
                return true;
              }
            });
          }
          return true;
        }
      });
      return true;
    }
  });
};

const introMainTable = (win)=>{
  win.submitYjlsb();
  nextTick(()=>{
    let btn = win.$('.xubox_layer .xubox_yes');
    if (btn.length) {
      btn.click();
      window.localStorage.setItem('zzsReady', 1);
      window.setTimeout(()=>{
        window.close();
      }, 2000);
      return true;
    }
  }, 1000);
};


export {
  handleSmallIndex,
  handleSmallStep1,
  handleSmallStep2,
  handleSmallStep3
};
